/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Enum.SearchGeneralEnum;
import dao.SanPhamCuaHangDAO;
import dao.SanPhamDAO;
import java.awt.Component;
import java.util.Date;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import pojo.SanPham;
import pojo.SanPhamCuaHang;

/**
 *
 * @author 0
 */
public class SanPhamCuaHangGUI extends javax.swing.JDialog {

    /**
     * Creates new form SanPhamCuaHangGUI
     */
    private SanPham selectedSanPham;
    private static Logger logger = Logger.getLogger(SanPhamCuaHangGUI.class);
    private JErrorProvider errorProvider;
    private MyEventListener ButtonOKClickedListener;

    public SanPhamCuaHangGUI(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        errorProvider=new JErrorProvider();
        SetTabOrder();
        FormatControl();
    }

    private void SetTabOrder() {
        IndexedFocusTraversalPolicy indexedFocusTraversalPolicy = new IndexedFocusTraversalPolicy();
        indexedFocusTraversalPolicy.addIndexedComponents(new Component[]{txtMaSanPham,btnSearchSanPham,txtTenSanPham,txtDonGia,btnLuu,btnThoat});
        setFocusTraversalPolicy(indexedFocusTraversalPolicy);
    }

    private void FormatControl() {
        Utility.FormatNumberStringTextfield(txtMaSanPham);
        Utility.FormatDecimalTextfield(txtDonGia);
    }
    
    private int CheckDatabeforeLuu(){
        int dem=0;
        errorProvider.clear();
        if(txtMaSanPham.getText().isEmpty()){
            errorProvider.setError(txtMaSanPham, "required");
            dem++;
        }
        return dem;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtMaSanPham = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtTenSanPham = new javax.swing.JTextField();
        btnSearchSanPham = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtDonGia = new javax.swing.JFormattedTextField();
        btnLuu = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm sản phẩm vào cửa hàng");
        setResizable(false);

        jLabel1.setText("Mã sản phẩm");

        txtMaSanPham.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtMaSanPhamFocusLost(evt);
            }
        });

        jLabel2.setText("Tên sản phẩm");

        txtTenSanPham.setEditable(false);

        btnSearchSanPham.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/SearchIconImage.gif"))); // NOI18N
        btnSearchSanPham.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchSanPhamActionPerformed(evt);
            }
        });

        jLabel3.setText("Đơn giá");

        txtDonGia.setEditable(false);

        btnLuu.setText("Lưu");
        btnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuuActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtMaSanPham)
                    .addComponent(txtTenSanPham)
                    .addComponent(txtDonGia, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))
                .addGap(26, 26, 26)
                .addComponent(btnSearchSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(103, Short.MAX_VALUE)
                .addComponent(btnLuu)
                .addGap(18, 18, 18)
                .addComponent(btnThoat)
                .addGap(85, 85, 85))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtMaSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnSearchSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTenSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLuu)
                    .addComponent(btnThoat))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchSanPhamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchSanPhamActionPerformed
        try {
            // TODO add your handling code here:
            DialogGeneralSeach dialogGeneralSeach = new DialogGeneralSeach(null, true, SearchGeneralEnum.SanPham);
            final int x = (getWidth() - dialogGeneralSeach.getWidth()) + getX();
            final int y = (getHeight() - dialogGeneralSeach.getHeight()) + getY();
            dialogGeneralSeach.setLocation(x, y);
            dialogGeneralSeach.setButtonOKClickedListenner(new MyEventListener() {
                @Override
                public void myEventOccured(Object evt) {
                    setSelectedSanPham((SanPham) evt);
                }
            });
            dialogGeneralSeach.setVisible(true);
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }//GEN-LAST:event_btnSearchSanPhamActionPerformed

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuuActionPerformed
        try {
            // TODO add your handling code here:
            if(ButtonOKClickedListener==null){
                return;
            }
            if(CheckDatabeforeLuu()>0){
                return;
            }
            SanPhamCuaHang sanPhamCuaHang=new SanPhamCuaHang(0, MainGUI.nguoiDung.getCuaHang(), selectedSanPham, 0, false, null, null);
            int id=SanPhamCuaHangDAO.Them(sanPhamCuaHang);
            sanPhamCuaHang.setId(id);
            JOptionPane.showMessageDialog(null, "Thêm thành công");
            ButtonOKClickedListener.myEventOccured(sanPhamCuaHang);
            dispose();
        } catch (Exception ex) {
            logger.error(new Date().toString(), ex);
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
        
    }//GEN-LAST:event_btnLuuActionPerformed

    private void txtMaSanPhamFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtMaSanPhamFocusLost
        // TODO add your handling code here:
        if (!txtMaSanPham.getText().isEmpty()) {
            if (selectedSanPham == null || Integer.parseInt(txtMaSanPham.getText()) != selectedSanPham.getId()) {
                try {
                    SanPham sanPham = (SanPham) SanPhamDAO.LayTheoID(SanPham.class, Integer.parseInt(txtMaSanPham.getText()));
                    if (sanPham == null) {
                        JOptionPane.showMessageDialog(this, "Không tìm thấy mã sản phẩm này trong hệ thống");
                    }
                    setSelectedSanPham(sanPham);
                } catch (Exception ex) {
                    logger.error(new Date().toString(), ex);
                    JOptionPane.showMessageDialog(this, ex.getMessage());
                }
            }
        } else {
            setSelectedSanPham(null);
        }
    }//GEN-LAST:event_txtMaSanPhamFocusLost
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLuu;
    private javax.swing.JButton btnSearchSanPham;
    private javax.swing.JButton btnThoat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JFormattedTextField txtDonGia;
    private javax.swing.JTextField txtMaSanPham;
    private javax.swing.JTextField txtTenSanPham;
    // End of variables declaration//GEN-END:variables

    /**
     * @param selecttedSanPham the selecttedSanPham to set
     */
    public void setSelectedSanPham(SanPham selectedSanPham) {
        this.selectedSanPham = selectedSanPham;
        if (selectedSanPham != null) {
            txtMaSanPham.setText(String.valueOf(selectedSanPham.getId()));
            txtTenSanPham.setText(selectedSanPham.getTenSanPham());
            txtDonGia.setValue(selectedSanPham.getDonGia());
        } else {
            txtMaSanPham.setText(null);
            txtTenSanPham.setText(null);
            txtDonGia.setValue(null);
        }
    }

    /**
     * @param ButtonOKClickedListener the ButtonOKClickedListener to set
     */
    public void setButtonOKClickedListener(MyEventListener ButtonOKClickedListener) {
        this.ButtonOKClickedListener = ButtonOKClickedListener;
    }
}
